package com.ccrfid.rmc6.repository.alarm;

import com.ccrfid.rmc6.entity.alarm.AlarmRecordEntity;
import com.ccrfid.rmc6.pojo.dto.report.AlarmTypeCountDto;
import com.ccrfid.rmc6.pojo.dto.report.DayAlarmCountDto;
import com.ccrfid.rmc6.repository.search.SearchableRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public interface AlarmRecordRepository extends Repository<AlarmRecordEntity, Long>,
        SearchableRepository<AlarmRecordEntity> {
    //
    AlarmRecordEntity getById(Long id);
    //
    AlarmRecordEntity save(AlarmRecordEntity entity);
    //
    Boolean existsById(Long id);
    //
    AlarmRecordEntity getTopByAlarmTypeAndCardNumberAndAlarmTimeAfter(Integer alarmType, Long cardNumber, Timestamp alarmTime);
    //
    AlarmRecordEntity getTopByAlarmTypeAndCardNumberAndClosed(Integer alarmType, Long cardNumber, Integer closed);

    @Query(nativeQuery = true, value =
            "select alarm_type as alarmType,alarm_type_name as alarmTypeName ,count(1) as count from alarm_record where location_id = :locationId " +
                    "and  alarm_time BETWEEN :startTime and :endTime group by alarm_type,alarm_type_name")
    List<AlarmTypeCountDto>  getAllAlarmRecord(@Param("locationId") Long locationId, @Param("startTime") Timestamp startTime, @Param("endTime") Timestamp endTime);
}
